package com.quanyan.user.service.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.user.mapper.NewsUnreadDetailsMapper;
import com.quanyan.user.mapper.TbNewsUnreadDetailsMapper;
import com.quanyan.user.mapper.TbNewsUnreadMapper;
import com.quanyan.user.model.db.TbNewsUnread;
import com.quanyan.user.model.db.TbNewsUnreadDetails;
import com.quanyan.user.model.db.TbNewsUnreadExample;
import com.quanyan.user.model.db.TbUser;
import com.quanyan.user.model.vo.request.ReqUnReadNews;
import com.quanyan.user.model.vo.request.UnReadParam;
import com.quanyan.user.model.vo.response.RespUnReadCount;
import com.quanyan.user.service.NewsUnReadService;
import com.quanyan.user.service.UserQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/22.
 */
@Service
public class NewsUnReadServiceImpl implements NewsUnReadService {

    @Autowired
    private TbNewsUnreadMapper tbNewsUnreadMapper;

    @Autowired
    private TbNewsUnreadDetailsMapper tbNewsUnreadDetailsMapper;

    @Autowired
    private UserQueryService userQueryService;

    @Autowired
    private NewsUnreadDetailsMapper newsUnreadDetailsMapper;

    public TbNewsUnread getTbNewsUnread(Integer uid){
        TbNewsUnreadExample example = new TbNewsUnreadExample();
        TbNewsUnreadExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        List<TbNewsUnread> lst =  tbNewsUnreadMapper.selectByExample(example);
        TbNewsUnread tbNewsUnread = null;
        if(lst !=null && lst.size() > 0) {
            tbNewsUnread = lst.get(0);
            tbNewsUnread.setUpdateTime(new Date());
        }
        return tbNewsUnread;
    }


    @Override
    @Transactional
    public void addNewsUnRead(UnReadParam unReadParam) {
        int uid = unReadParam.getUid();
        TbNewsUnreadDetails tbNewsUnreadDetails = new TbNewsUnreadDetails();

       TbNewsUnread tbNewsUnread = this.getTbNewsUnread(uid);
        if(tbNewsUnread !=null){
            tbNewsUnread.setUpdateTime(new Date());
            if (NewsUnReadType.comment == unReadParam.getType().intValue()){
                tbNewsUnread.setCommentCount( tbNewsUnread.getCommentCount() + 1);
                tbNewsUnreadDetails.setType(NewsUnReadType.comment.byteValue());
            } else if (NewsUnReadType.praise == unReadParam.getType().intValue()){
                tbNewsUnread.setPraiseCount(tbNewsUnread.getPraiseCount() + 1);
                tbNewsUnreadDetails.setType(NewsUnReadType.praise.byteValue());
            } else if (NewsUnReadType.at == unReadParam.getType().intValue()){ //at用户
                tbNewsUnread.setCommentCount(tbNewsUnread.getCommentCount() + 1);
                tbNewsUnreadDetails.setType(NewsUnReadType.comment.byteValue());
            }
            tbNewsUnreadMapper.updateByPrimaryKeySelective(tbNewsUnread);
        }else {
            tbNewsUnread = new TbNewsUnread();
            tbNewsUnread.setUid(uid);
            if (NewsUnReadType.comment == unReadParam.getType().intValue()){
                tbNewsUnread.setCommentCount( 1);
                tbNewsUnreadDetails.setType(NewsUnReadType.comment.byteValue());
            } else if (NewsUnReadType.praise == unReadParam.getType().intValue()){
                tbNewsUnread.setPraiseCount(1);
                tbNewsUnreadDetails.setType(NewsUnReadType.praise.byteValue());
            } else if (NewsUnReadType.at == unReadParam.getType().intValue()){ //at用户
                tbNewsUnread.setCommentCount(tbNewsUnread.getCommentCount() + 1);
                tbNewsUnreadDetails.setType(NewsUnReadType.comment.byteValue());
            }
            tbNewsUnread.setUpdateTime(new Date());
            tbNewsUnread.setCreateTime(new Date());
            tbNewsUnread.setIsDelete(Constants.IS_DELETE_FALSE);
            tbNewsUnreadMapper.insertSelective(tbNewsUnread);
        }

        TbUser fUser = userQueryService.getUser(unReadParam.getfUid()); // 消息发送者
        if (fUser != null ){
            tbNewsUnreadDetails.setfIcon(fUser.getIcon());
            tbNewsUnreadDetails.setfNickName(fUser.getNickName());
        }
        tbNewsUnreadDetails.setToUid(unReadParam.getUid());
        tbNewsUnreadDetails.setfUid(unReadParam.getfUid());
        tbNewsUnreadDetails.setCreateTime(new Date());
        tbNewsUnreadDetails.setIsDelete(Constants.IS_DELETE_FALSE);
        tbNewsUnreadDetails.setUpdateTime(new Date());
        tbNewsUnreadDetails.setNewsImg(unReadParam.getDynamicImg());
        tbNewsUnreadDetails.setNewsId(unReadParam.getNewsId())  ;

        if (NewsUnReadType.praise != unReadParam.getType().intValue())
            tbNewsUnreadDetails.setContent(unReadParam.getContent());

        tbNewsUnreadDetailsMapper.insertSelective(tbNewsUnreadDetails);

        if (NewsUnReadType.at == unReadParam.getType().intValue()) {
            Integer atUid = unReadParam.getAtUid(); // at 用户
            TbNewsUnreadDetails atUnreadDetail = new TbNewsUnreadDetails();
            if (fUser != null ){
                atUnreadDetail.setfIcon(fUser.getIcon());
                atUnreadDetail.setfNickName(fUser.getNickName());
            }
            atUnreadDetail.setfUid(unReadParam.getfUid());
            atUnreadDetail.setToUid(atUid);
            atUnreadDetail.setCreateTime(new Date());
            atUnreadDetail.setType(NewsUnReadType.at.byteValue());
            atUnreadDetail.setIsDelete(Constants.IS_DELETE_FALSE);
            atUnreadDetail.setUpdateTime(new Date());
            atUnreadDetail.setNewsImg(unReadParam.getDynamicImg());
            atUnreadDetail.setNewsId(unReadParam.getNewsId())  ;


            TbNewsUnread atNewsUnread = this.getTbNewsUnread(atUid);
            if (atNewsUnread != null ){
                TbNewsUnread updateAtNewsUnread = new TbNewsUnread();
                updateAtNewsUnread.setId(atNewsUnread.getId());
                updateAtNewsUnread.setAtCount(atNewsUnread.getAtCount() + 1);
                tbNewsUnreadMapper.updateByPrimaryKeySelective(updateAtNewsUnread);
            }else {
                atNewsUnread = new TbNewsUnread();
                atNewsUnread.setUid(atUid);
                atNewsUnread.setAtCount(1);
                atNewsUnread.setPraiseCount(0);
                atNewsUnread.setCommentCount(0);
                atNewsUnread.setUpdateTime(new Date());
                atNewsUnread.setCreateTime(new Date());
                atNewsUnread.setIsDelete(Constants.IS_DELETE_FALSE);
                tbNewsUnreadMapper.insertSelective(atNewsUnread);
            }

            tbNewsUnreadDetailsMapper.insertSelective(atUnreadDetail);

        }

    }


    public RespUnReadCount queryUnReadNewsCount(Integer uid){
        TbNewsUnreadExample example = new TbNewsUnreadExample();
        TbNewsUnreadExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        List<TbNewsUnread> lst =  tbNewsUnreadMapper.selectByExample(example);
        RespUnReadCount respUnReadCount = new RespUnReadCount();
        if(lst !=null && lst.size() > 0){
            TbNewsUnread tbNewsUnread = lst.get(0);
            respUnReadCount.setCommentCount(tbNewsUnread.getCommentCount());
            respUnReadCount.setAtCount(tbNewsUnread.getAtCount());
            respUnReadCount.setPraiseCount(tbNewsUnread.getPraiseCount());
            respUnReadCount.setFlag();
        }
        return respUnReadCount;
    }

    @Override
    public void clearNewsUnRead(Integer uid){
        TbNewsUnreadExample example = new TbNewsUnreadExample();
        TbNewsUnreadExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);

        List<TbNewsUnread> lst =  tbNewsUnreadMapper.selectByExample(example);
        if(lst !=null && lst.size() > 0){
            TbNewsUnread tbNewsUnread = lst.get(0);
            tbNewsUnread.setUpdateTime(new Date());
            tbNewsUnread.setAtCount(0);
            tbNewsUnread.setPraiseCount(0);
            tbNewsUnread.setCommentCount(0);
            tbNewsUnreadMapper.updateByPrimaryKeySelective(tbNewsUnread);
        }
    }



    public  PageObj<List<TbNewsUnreadDetails>>  queryUnReadNews(ReqUnReadNews reqUnReadNews){
        Page page = new Page();
        int pageIndex = reqUnReadNews.getPageNum() + 1;
        page.setCurrentPage(pageIndex);
        page.setShowCount(reqUnReadNews.getPageSize());
        List<TbNewsUnreadDetails> list =  newsUnreadDetailsMapper.findAllUnReadNews(page, reqUnReadNews.getUid());
        if (pageIndex > page.getTotalPage()) {// 大于最后一页
            list = new ArrayList<>();
            page.setTotalPage(0);
            page.setTotalResult(0);
            page.setCurrentResult(0);
        }
        PageObj<List<TbNewsUnreadDetails>> result = PageObj.create(page.getTotalResult(), page.getCurrentPage(), page.getShowCount(), list);
        this.clearNewsUnRead(reqUnReadNews.getUid());
        return result;
    }


}
